package com.twu.biblioteca.menu;

import com.twu.biblioteca.Librarian;

public class LoginMenu implements Menu {
    private Librarian librarian;
    private String userNumber = "";
    private String password = "";
    private int state = 0;
    public String parentMenu = "";

    public LoginMenu(Librarian librarian) {
        this.librarian = librarian;
        showMenu();
    }

    @Override
    public void showMenu() {
        System.out.println("Please Login First");
        System.out.println("Library Number:");
    }

    @Override
    public Menu processInput(String input) {
        if (state == 1) {
            password = input;
            if (parentMenu.equals("CheckoutMenu")) {
                return getMenu(new CheckoutMenu(librarian));
            } else if (parentMenu.equals("ReturnMenu")) {
                return getMenu(new ReturnMenu(librarian));
            }
            return getMenu(new LibraryMenu(librarian));
        }
        userNumber = input;
        System.out.println("Password:");
        state++;
        return this;
    }

    private Menu getMenu(Menu menu) {
        if (librarian.login(userNumber,password)) {
            System.out.println("Welcome back! "+librarian.currentUser.getName());
            librarian.hasLogIn = true;
            return menu;
        }
        else {
            System.out.println("Username or Password is not correct. Please enter again");
            System.out.println("Username:");
            state = 0;
            return this;
        }
    }
}
